package com.rh.bmw4s.ui;

import android.content.Context;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.rh.bmw4s.utills.ActivityCollector;
import com.rh.bmw4s.utills.NetWorkStateReceiver;

//封装基类activity
public class BaseActivity extends AppCompatActivity {
    private Context context;
    NetWorkStateReceiver netWorkStateReceiver;

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
       // ActivityCollector.getInstances().addActivity(this);

        //如果是小米手机则隐藏状态栏，否则显示沉浸式状态栏
//        if (JudgePhoneBrand.SYS_MIUI.equals(JudgePhoneBrand.getSystem())) {
//            requestWindowFeature(Window.FEATURE_NO_TITLE);
//            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
//        } else {
//        StatusBarUtil.setColor(this, getResources().getColor(R.color.main_theme_color), 55);
//        }
       /* if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            StatusBarUtil.setColor(this,  getResources().getColor(R.color.main_theme_color,getTheme()));
        }else{
            StatusBarUtil.setColor(this, getResources().getColor(R.color.main_theme_color));
        }*/
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        ActivityCollector.getInstances().addActivity(this);
    }

    //注册广播监听网络变化
    @Override
    protected void onResume() {
        super.onResume();
        if (netWorkStateReceiver == null) {
            netWorkStateReceiver = new NetWorkStateReceiver();
        }
        IntentFilter filter = new IntentFilter();
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(netWorkStateReceiver, filter);
    }

    //onPause()方法注销
    @Override
    protected void onPause() {
        unregisterReceiver(netWorkStateReceiver);
        super.onPause();
    }

    // 内存紧张时回收图片资源
    @Override
    public void onLowMemory() {
        super.onLowMemory();
        Glide.get(this).clearMemory();
        /*MyThreadPool.submit(new Runnable() {
            @Override
            public void run() {
                Glide.get(context).clearDiskCache();
            }
        });*/
    }

    // 内存紧张时回收图片资源 API4.0
    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
        Glide.get(this).trimMemory(level);
    }

    public void invokeGc() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    finalize();
                } catch (Throwable e) {
                }
                // 提醒系统及时回收
                System.gc();
            }
        }).start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        invokeGc();
        ActivityCollector.getInstances().finishActivity(this);
    }

}
